package com.qianying.controller.admin;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qianying.comment.CommonResult;
import com.qianying.entity.ShopInfo;
import com.qianying.service.ShopInfoService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 门店信息控制器
 * 提供门店信息的HTTP接口，处理前端请求并调用对应服务层方法
 *
 * @author check
 * @date 2025-08-01
 */
@RestController
@RequestMapping("/admin/shopInfo")
@Tag(name = "客户:门店信息管理", description = "门店信息的增删改查操作接口")
public class ShopInfoController {

    @Autowired
    private ShopInfoService shopInfoService;

    /**
     * 根据ID查询门店信息
     *
     * @param id 门店ID
     * @return 包含门店详情的通用结果
     */
    @GetMapping("/getById")
    @Operation(summary = "查询门店详情", description = "根据ID查询门店完整信息，包含关联的大区和省市区名称")
    public CommonResult<ShopInfo> getById(
            @Parameter(description = "门店ID", required = true)
            @RequestParam Long id) {
        return shopInfoService.getById(id);
    }

    /**
     * 根据客户ID、门店名称和状态分页查询门店列表（默认客户ID为3）
     *
     * @param pageNum    页码（默认1）
     * @param pageSize   每页条数（默认10）
     * @param customerId 客户ID（默认3）
     * @param shopName   门店名称（可选，用于模糊查询）
     * @param status     门店状态（可选，精确查询）
     * @return 包含分页门店列表的通用结果
     */
    @GetMapping("/pageByCustomerId")
    @Operation(summary = "按客户分页查询门店", description = "根据客户ID、门店名称和状态分页查询所属门店，支持条件组合查询")
    public CommonResult<IPage<ShopInfo>> getPageByCustomerId(
            @Parameter(description = "页码，默认1")
            @RequestParam(defaultValue = "1") Integer pageNum,
            @Parameter(description = "每页条数，默认10")
            @RequestParam(defaultValue = "10") Integer pageSize,
            @Parameter(description = "客户ID，默认3")
            @RequestParam(defaultValue = "3") Long customerId,
            @Parameter(description = "门店名称，模糊查询，可选")
            @RequestParam(required = false) String shopName,
            @Parameter(description = "门店状态，可选，例如：1-正常，2-关闭")
            @RequestParam(required = false) Integer status) {

        Page<ShopInfo> page = new Page<>(pageNum, pageSize);
        return shopInfoService.getPageByCustomerId(page, customerId, shopName, status);
    }

    /**
     * 根据客户ID、门店名称和状态分页查询门店列表（默认客户ID为3）
     *
     * @param pageNum    页码（默认1）
     * @param pageSize   每页条数（默认10）
     * @param shopName   门店名称（可选，用于模糊查询）
     * @param status     门店状态（可选，精确查询）
     * @return 包含分页门店列表的通用结果
     */
    @GetMapping("/getPageAllShopList")
    @Operation(summary = "获取所有的门店信息", description = "根据门店名称和状态分页查询所属门店，支持条件组合查询")
    public CommonResult<IPage<ShopInfo>> getPageAllShopList(
            @Parameter(description = "页码，默认1")
            @RequestParam(defaultValue = "1") Integer pageNum,
            @Parameter(description = "每页条数，默认10")
            @RequestParam(defaultValue = "10") Integer pageSize,
            @Parameter(description = "门店名称，模糊查询，可选")
            @RequestParam(required = false) String shopName,
            @Parameter(description = "门店状态，可选，例如：1-正常，2-关闭")
            @RequestParam(required = false) Integer status) {

        Page<ShopInfo> page = new Page<>(pageNum, pageSize);
        return shopInfoService.getPageAllShopList(page, shopName, status);
    }

    /**
     * 新增门店信息
     *
     * @param shopInfo 门店信息实体（JSON格式）
     * @return 包含新增门店信息的通用结果
     */
    @PostMapping("/save")
    @Operation(summary = "新增门店信息",
            parameters = {
                    @Parameter(name = "id", description = "主键ID（自增）", example = "1"),
                    @Parameter(name = "customerId", description = "客户Id", example = "1001"),
                    @Parameter(name = "customerName", description = "大区名称（非数据库字段，关联查询获取）", example = "华东大区"),
                    @Parameter(name = "regionId", description = "所属大区ID（关联region表）", example = "201"),
                    @Parameter(name = "provinceId", description = "省份ID（关联china_area表）", example = "3001"),
                    @Parameter(name = "cityId", description = "城市ID（关联china_area表）", example = "40001"),
                    @Parameter(name = "districtId", description = "区域ID（关联china_area表）", example = "500001"),
                    @Parameter(name = "regionName", description = "大区名称（非数据库字段，关联查询获取）", example = "华东大区"),
                    @Parameter(name = "provinceName", description = "省份名称（非数据库字段，关联查询获取）", example = "江苏省"),
                    @Parameter(name = "cityName", description = "城市名称（非数据库字段，关联查询获取）", example = "南京市"),
                    @Parameter(name = "districtName", description = "区域名称（非数据库字段，关联查询获取）", example = "鼓楼区"),
                    @Parameter(name = "shopName", description = "店铺名称", example = "科技数码旗舰店"),
                    @Parameter(name = "shopAbbreviation", description = "店铺简称", example = "科技数码店"),
                    @Parameter(name = "shopNumber", description = "店铺号", example = "SH2025001"),
                    @Parameter(name = "registrationTime", description = "注册时间", example = "2025-01-15 09:30:00"),
                    @Parameter(name = "customerService", description = "对接客服", example = "张三"),
                    @Parameter(name = "districtManager", description = "对接区管", example = "李四"),
                    @Parameter(name = "mall", description = "所属商场", example = "中央商场"),
                    @Parameter(name = "invoiceTitle", description = "开票抬头", example = "科技数码有限公司"),
                    @Parameter(name = "taxNumber", description = "税号", example = "91320106XXXXXXXXXX"),
                    @Parameter(name = "defaultSettlementMethod", description = "默认结算方式", example = "月结"),
                    @Parameter(name = "defaultReceiptMethod", description = "默认验收方式", example = "现场验收"),
                    @Parameter(name = "finance", description = "财务联系人", example = "王五"),
                    @Parameter(name = "addressDetail", description = "详细地址", example = "中山路123号3楼"),
                    @Parameter(name = "needPublicInsurance", description = "是否需要购买公众险（0-否，1-是）", example = "1"),
                    @Parameter(name = "needWorkerInsurance", description = "是否需要购买师傅意外险（0-否，1-是）", example = "1"),
                    @Parameter(name = "shopManagerName", description = "店长姓名", example = "赵六"),
                    @Parameter(name = "shopManagerPhone", description = "店长联系电话", example = "13800138000"),
                    @Parameter(name = "shopManagerFixedPhone", description = "店长固定电话", example = "025-88888888"),
                    @Parameter(name = "floorHeight", description = "层高", example = "3.5米"),
                    @Parameter(name = "nightConstruction", description = "是否夜间施工", example = "是"),
                    @Parameter(name = "lastOpeningDate", description = "最近开业日期", example = "2025-05-20 10:00:00"),
                    @Parameter(name = "ladderHeight", description = "梯子高度", example = "4米"),
                    @Parameter(name = "certificateFee", description = "办证费", example = "500.00"),
                    @Parameter(name = "quoteApprovalRequired", description = "是否需要报价审批（0-否，1-是）", example = "1"),
                    @Parameter(name = "quoteApprovalAuditor", description = "报价审批审核人", example = "钱七"),
                    @Parameter(name = "quoteApprovalAuditorPhone", description = "报价审批审核人联系电话", example = "13900139000"),
                    @Parameter(name = "quoteApprovalAuditorFixedPhone", description = "报价审批审核人固定电话", example = "025-77777777"),
                    @Parameter(name = "quoteApprovalApprover", description = "报价审批核准人", example = "孙八"),
                    @Parameter(name = "quoteApprovalApproverPhone", description = "报价审批核准人联系电话", example = "13700137000"),
                    @Parameter(name = "quoteApprovalApproverFixedPhone", description = "报价审批核准人固定电话", example = "025-66666666"),
                    @Parameter(name = "acceptanceApprovalRequired", description = "是否需要验收审批（0-否，1-是）", example = "1"),
                    @Parameter(name = "acceptanceApprovalAuditor", description = "验收审批审核人", example = "周九"),
                    @Parameter(name = "acceptanceApprovalAuditorPhone", description = "验收审批审核人联系电话", example = "13600136000"),
                    @Parameter(name = "acceptanceApprovalAuditorFixedPhone", description = "验收审批审核人固定电话", example = "025-55555555"),
                    @Parameter(name = "acceptanceApprovalApprover", description = "验收审批核准人", example = "吴十"),
                    @Parameter(name = "acceptanceApprovalApproverPhone", description = "验收审批核准人联系电话", example = "13500135000"),
                    @Parameter(name = "acceptanceApprovalApproverFixedPhone", description = "验收审批核准人固定电话", example = "025-44444444"),
                    @Parameter(name = "status", description = "状态", example = "1"),
                    @Parameter(name = "createTime", description = "创建时间（自动生成）", example = "2025-08-01 10:00:00"),
                    @Parameter(name = "updateTime", description = "更新时间（自动更新）", example = "2025-08-01 15:30:00")
            },
            description = "添加新的门店信息，客户名称和门店名称为必填项")
    public CommonResult<ShopInfo> save(
            @Parameter(description = "门店信息实体，JSON格式", required = true)
            @RequestBody ShopInfo shopInfo) {
        return shopInfoService.save(shopInfo);
    }

    /**
     * 根据ID更新门店信息
     *
     * @param shopInfo 门店信息实体（含ID，JSON格式）
     * @return 包含更新结果的通用结果
     */
    @PutMapping("/updateById")
    @Operation(summary = "更新门店信息", description = "根据ID更新门店信息，ID、客户名称和门店名称为必填项")
    public CommonResult<?> updateById(
            @Parameter(description = "门店信息实体（含ID），JSON格式", required = true)
            @RequestBody ShopInfo shopInfo) {
        return shopInfoService.updateById(shopInfo);
    }

    /**
     * 根据ID删除门店信息
     *
     * @param id 门店ID
     * @return 包含删除结果的通用结果
     */
    @DeleteMapping("/removeById")
    @Operation(summary = "删除门店信息", description = "根据ID删除门店信息，删除前请确保该门店无关联数据")
    public CommonResult<?> removeById(
            @Parameter(description = "门店ID", required = true)
            @RequestParam Long id) {
        return shopInfoService.removeById(id);
    }
}
